﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using ParallelRuntimeLibrary;
using ParallelRuntimeLibrary.Collections;

namespace TestProject
{   //bm1 (Two threads) for ConcurrentBag : 7000  
    //bm1 (Two threads) for Stack : 7150
    //bm1 (Four threads) for ConcurrentBag : 14350  
    //bm1 (Four threads) for Stack : 14470
    public static class ConcurrentBagTester
    {
        static ConcurrentStack<string> X;

        public static void Start()
        {
            X = new ConcurrentStack<string>(false, ConcurrentModes.LockFree);

            Task[] Ts = new Task[4];
            for (int i = 0; i < Ts.Length; i++)
                Ts[i] = new Task(ThreadWork, i.ToString());

            Stopwatch SW = new Stopwatch();
            SW.Start();

            TaskManager.WaitAll(Ts);

            SW.Stop();

            Console.WriteLine(SW.ElapsedMilliseconds);
            Console.ReadLine();
        }

        public static void ThreadWork(object str)
        {
            for (int i = 0; i < 2000000; i++)
                X.Push((str as string) + i.ToString());

            for (int i = 0; i < 1000000; i++)
            {
                string tmp;
                X.Pop(out tmp);
                X.Push((str as string) + "  :  " + i.ToString());
            }

            while (true)
            {
                
                string tmp;
                if (!X.Pop(out tmp))
                    return;
            }
        }
    }
}
